package com.qiguliuxing.dts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.support.JacksonUtil;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsAccountTrace;
import com.qiguliuxing.dts.db.domain.DtsUser;
import com.qiguliuxing.dts.service.base.DtsAccountService;
import com.qiguliuxing.dts.service.base.DtsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 佣金业务接口
 * 
 * @since 1.0.0
 * @author qiguliuxing
 * @QQ 623659388
 */
@RestController
@RequestMapping("/admin/brokerage")
@Validated
@Api(tags = ApiAdminConsts.Brokerage.PARENT )
public class AdminBrokerageController {
	private static final Logger logger = LoggerFactory.getLogger(AdminBrokerageController.class);

	@Autowired
	private DtsAccountService accountService;
	
	@Autowired
	private DtsUserService userService;

	@RequiresPermissions("admin:brokerage:list")
	@RequiresPermissionsDesc(menu = { "用户管理", "佣金管理" }, button = "查询")
	@ApiOperation(value= ApiAdminConsts.Brokerage.LIST)
	@GetMapping("/list")
	@WebLog(description = ApiAdminConsts.Brokerage.LIST, excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "用户名称") String username,
					   @ApiParam(value = "手机号") String mobile,
					   @ApiParam(value = "状态数组") @RequestParam(required = false) List<Byte> statusArray,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
		List<DtsUser> userList = userService.queryDtsUserListByNickname(username, mobile);
		List<DtsAccountTrace> traceList = accountService.querySelectiveTrace(userList,statusArray, page, limit, sort, order);
		long total = PageInfo.of(traceList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("traceList", traceList);
		return ResponseUtils.ok(data);
	}

	@RequiresPermissions("admin:brokerage:approve")
	@RequiresPermissionsDesc(menu = { "用户管理", "佣金管理" }, button = "审批销账")
	@PostMapping("/approve")
	@ApiOperation(value= ApiAdminConsts.Brokerage.APPROVE,notes = "body参数：{id:'',traceMsg:'',status:'状态'}")
	@WebLog(description = ApiAdminConsts.Brokerage.APPROVE,persistence = true)
	public Object approve(@RequestBody String body) {
		Integer traceId = JacksonUtil.parseInteger(body, "id");
		String traceMsg = JacksonUtil.parseString(body, "traceMsg");
		Byte status = JacksonUtil.parseByte(body, "status");
		boolean approveResult = accountService.approveAccountTrace(traceId,status, traceMsg);
		
		if (!approveResult) {
			logger.info("用户管理->佣金管理->审批销账失败：{}", "审批处理错误！");
			return ResponseUtils.updatedDataFailed();
		}
		return ResponseUtils.ok();
	}
}
